import request from '../utils/request';

/**
 * 创建产品类型
 * @param {string} name 
 * @param {string} description 
 * @param {string} imageUrl 
 */

export function createProductTypeApi(
  name,
  description,
  imageUrl
) {
  return request({
    url: '/product_types',
    method: 'post',
    data: Object.assign(
      {
        name: name
      },
      description ? { description } : null,
      imageUrl ? { image_url: imageUrl } : null
    )
  });
} 

/**
 * 删除产品类型
 * @param {string} uuid 
 */

export function deleteProductTypeApi(uuid) {
  return request({
    url: '/product_types/' + uuid,
    method: 'delete'
  });
}

/**
 * 更新产品类型
 * @param {string} uuid 
 * @param {string} name 
 * @param {string} description 
 * @param {string} imageUrl 
 */

export function updateProductTypeApi(
  uuid,
  name,
  description,
  imageUrl
) {
  return request({
    url: '/product_types/' + uuid,
    method: 'put',
    data: Object.assign(
      {
        name: name,
        image_url: imageUrl
      },
      description ? { description } : null
    )
  });
}

/**
 * 获取产品类型列表 
 * @param {number} pageIndex 
 * @param {number} pageSize 
 * @param {string} sortType 
 * @param {string} orderBy 
 * @param {string} name 
 */
export function getProductTypeListApi(
  pageIndex,
  pageSize,
  sortType,
  orderBy,
  name
) {
  return request({
    url: '/product_types',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType,
        order_by: orderBy  
      },
      name ? { name } : null
    )
  });
}

/**
 * 获取产品类型详情
 * @param {string} uuid 
 */
export function getProductTypeDetailApi(uuid) {
  return request({
    url: '/product_types/' + uuid,
    method: 'get'
  });
}

export function queryProductTypeListApi(name, take) {
  return request({
    url: '/product_types/query',
    method: 'get',
    params: { name, take }
  });
}
